// hyb_yl/wodetiwen/wodetiwen.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: ["已回答", "未回答"],
    current: 0,
  },
  tab: function(e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    var index = e.currentTarget.dataset.index
    if (index == 1) {
      console.log('未回答')
      app.util.request({
        'url': 'entry/wxapp/mywquestion',
        data: {
          openid: openid
        },
        success: function(res) {
          console.log(res)
          var newarr = res.data.data;
          that.setData({
            myquestionsss: newarr,
          })

        }
      })
    } else {
      console.log('已经回答')
      app.util.request({
        'url': 'entry/wxapp/mywyiquestion',
        data: {
          openid: openid
        },
        success: function(res) {
          console.log(res)
          var newarr = res.data.data;
          that.setData({
            myquestion: newarr,
          })

        }
      })
    }
    this.setData({
      current: e.currentTarget.dataset.index
    })
  },
  questionsZan: function(e) {
    let that = this;
    let wenda = that.data.wenda;
    let index = e.currentTarget.dataset.index;
    if (wenda[index].zan == true) {
      wx.showToast({
        title: '点赞成功',
      })
      wenda[index].zan = false
    } else {
      wx.showToast({
        title: '已取消点赞',
      })
      wenda[index].zan = true
    }
    that.setData({
      wenda: wenda
    })
  },
  daihuida: function(e) {
    var that = this
    var yuedu = parseInt(e.currentTarget.dataset.yuedu)
    if (yuedu == 0) {
      wx.showToast({
        image: '/hyb_yl//images/err.png',
        title: '等待医生回答',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    var doctz = options.doctz
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var openid = wx.getStorageSync('openid')
    //我的提问(已回答)
    app.util.request({
      'url': 'entry/wxapp/myquestion',
      data: {
        openid: openid
      },
      success: function(res) {
        console.log(res)
        var newarr = res.data.data;
        that.setData({
          myquestion: newarr,
          doctz: doctz
        })
      }
    })
    //未回答

  },
  // previewImage: function (e) {
  //   var current = e.target.dataset.src;
  //   wx.previewImage({
  //     current: current, // 当前显示图片的http链接  
  //     urls: this.data.url // 需要预览的图片http链接列表  
  //   })
  // },  
  previewImage: function(e) {
    var that = this;
    var current = e.currentTarget.dataset.src;
    var qid = e.currentTarget.dataset.qid;
    var qusetiontype = that.data.myquestion;
    var imgs = [];
    for (var i = 0; i < qusetiontype.length; i++) {
      if (qusetiontype[i].qid == qid) {
        imgs = qusetiontype[i].user_picture;
      }
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: imgs // 需要预览的图片http链接列表  
    })

  },
  previewImages: function(e) {
    var that = this;
    var current = e.currentTarget.dataset.src;
    var qid = e.currentTarget.dataset.qid;
    var qusetiontype = that.data.myquestionsss;
    var imgs = [];
    for (var i = 0; i < qusetiontype.length; i++) {
      if (qusetiontype[i].qid == qid) {
        imgs = qusetiontype[i].user_picture;
      }
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: imgs // 需要预览的图片http链接列表  
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var that = this
    var doctz = that.data.doctz
    if (doctz == 1) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 3];
      prevPage.setData({
        overtiwen: 1
      })
      wx.navigateBack({
        detail: 2
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  answerDetailClick: function(e) {
    console.log(e)
    var zid = e.currentTarget.dataset.zid
    var user_openid = e.currentTarget.dataset.openid
    var qid = e.currentTarget.dataset.qid
    var fromuser = e.currentTarget.dataset.fromuser
    // wx.navigateTo({
    //   url: '/hyb_yl/doc_answer_detail/doc_answer_detail',
    // })
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/myda/myda?zid=' + zid + '&user_openid=' + user_openid + '&qid=' + qid + '&fromuser=' + fromuser,
    })
  },
})